package com.bawei.recyclerview;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bawei.recyclerview.databinding.ItemBinding;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @Author : yaotianxue
 * @Time : On 2023/9/13 13:37
 * @Description : UserAdapter
 */
//1:自定义类继承RecyclerView.Adapter
//3：泛型添加对应的ViewHolder并重写方法
public class UserAdapter2 extends RecyclerView.Adapter<UserAdapter2.UserViewHolder> {
    private List<User> mList;

    public UserAdapter2(List<User> list) {
        mList = list;
    }
    //创建viewholder
    @NonNull
    @Override
    public UserViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemBinding itemBinding  = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item, parent, false);
        return new UserViewHolder(itemBinding);
    }
    //绑定数据
    @Override
    public void onBindViewHolder(@NonNull UserViewHolder holder, int position) {
        holder.mItemBinding.setUser(mList.get(position));
    }
    //返回个数
    @Override
    public int getItemCount() {
        return mList.size();
    }

    //2：自定义Viewholder继承RecyclerView.ViewHolder
    class UserViewHolder extends RecyclerView.ViewHolder{
        ItemBinding mItemBinding;
        //构造中传入的是ItemBinding
        public UserViewHolder(ItemBinding itemBinding) {
            super(itemBinding.getRoot());//返回布局View
            mItemBinding = itemBinding;
        }
    }
}
